//
//  RecommendScrollView.swift
//  swiftDemo
//
//  Created by Gerry on 2021/12/15.
//

import UIKit


class RecommendScrollView: UIView {
    
    var hotCates : [HotCateModel]? {
        didSet {
            hotCates?.removeFirst()
            hotCates?.removeFirst()
            
            collectionView.reloadData()
        }
    }
    
    lazy var collectionView : UICollectionView = {
        let layout = UICollectionViewFlowLayout.init()
        layout.scrollDirection = .horizontal
        layout.minimumInteritemSpacing = 10
        layout.minimumLineSpacing = 0;
        layout.itemSize = CGSize.init(width: 90, height: 90)
        
        let collectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: layout)
        collectionView.backgroundColor = .white
        collectionView.dataSource = self
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
        collectionView.register(RecommendCell.self, forCellWithReuseIdentifier: "RecommendCell")
        return collectionView
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension RecommendScrollView {
    func setupUI() {
        self.addSubview(collectionView)
        
        collectionView.snp.makeConstraints { make in
            make.edges.equalTo(0)
        }
    }
}


extension RecommendScrollView : UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return hotCates?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier:"RecommendCell", for: indexPath) as! RecommendCell
        cell.hotModel = hotCates?[indexPath.item]
        return cell
    }
}
